home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Light ROM 2
/
LIGHT-ROM 2 (Amiga Library Services)(1995).iso
/
programs
/
pc
/
l_parser
/
readraw.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-23
|
7KB
|
194 lines
/*
READRAW.C
Copyright (C) 1994 Earl C. Terwilliger
158 Eades Drive
Irvine, KY 40336
Phone (606)-723-5718
Internet: aisterwi@acs.eku.edu
*/
#define DXF_VERTICES 4
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <sys\types.h>
#include <sys\stat.h>
int fp1;
static unsigned long polygons = 0l, totpoints = 0l, nonpols = 0l;
static unsigned long triangles = 0l, twoptpols = 0l;
static unsigned int xyccpoly=0,yzccpoly=0,xzccpoly=0;
static unsigned int xycpoly=0,yzcpoly=0,xzcpoly=0;
static unsigned int xypols=0,yzpols=0,xzpols=0;
static double area;
#pragma pack(1)
static struct {
unsigned int layer;
unsigned int vertices;
unsigned int color;
unsigned long index[4];
unsigned int unique;
float xyz[4][3];
} POLY;
main(argc,argv)
int argc;
char *argv[];
{
unsigned int c,d,e;
char infile[64];
if (argc < 2) syntax(argv[0]);
strcpy(infile,argv[1]);
if (!strchr(infile,'.')) strcat(infile,".RAW");
if (!(fp1 = open(infile,O_BINARY|O_RDONLY))) {
printf("Cannot open input file %s!\n",infile);
exit(1);
}
while(read(fp1,&POLY,sizeof(POLY))) {
polygons += 1;
totpoints += POLY.vertices;
if (POLY.vertices == 3) ++triangles;
if (POLY.vertices == 2) ++twoptpols;
printf("Layer=%d Color=%d Vertices=%d Index=%4ld %4ld %4ld %4ld Unique=",
POLY.layer,POLY.color,POLY.vertices,
POLY.index[0],POLY.index[1],POLY.index[2],POLY.index[3]);
for(c=0;c<DXF_VERTICES;++c) {
if (!(POLY.unique & (0x01<<c))) continue;
printf("%d ",c+1);
}
c = poly_area_xy();
if (area < 0) printf("\nPolygon is clockwise on the ");
if (area > 0) printf("\nPolygon is counterclockwise on the ");
switch (c) {
case 0:
printf("\nPolygon not visible (2-point polygon?)\n");
break;
case 1:
printf("XY plane\n");
break;
case 2:
printf("YZ plane\n");
break;
case 3:
printf("XZ plane\n");
break;
default:
printf("\n");
break;
}
for(c=0;c<4;++c)
printf("x=%12.6f y=%12.6f z=%12.6f\n",POLY.xyz[c][0],POLY.xyz[c][1],POLY.xyz[c][2]);
}
close(fp1);
printf("\nPoints %ld\nPolygons %ld ",totpoints,polygons);
printf("[%ld triangles] [%ld 2-point polygons]\n",triangles,twoptpols);
printf("Front [xy] Polygons %2u [%2u clockwise] [%2u counterclockwise]\n",
xypols,xycpoly,xyccpoly);
printf("Side [yz] Polygons %2u [%2u clockwise] [%2u counterclockwise]\n",
yzpols,yzcpoly,yzccpoly);
printf("Top [xz] Polygons %2u [%2u clockwise] [%2u counterclockwise]\n",
xzpols,xzcpoly,xzccpoly);
printf("Non planar Polygons %2u [Includes 2-point polygons]\n",nonpols);
printf("\nProgram complete.\n");
exit(0);
}
syntax(ptr)
char *ptr;
{
printf("Syntax: %s infile[.RAW]\n",ptr);
exit(99);
}
/*
area = 0.5 * ( ( x[0] * y[1] ) + ( x[1] * y[2] ) + ( x[2] * y[0] ) -
( x[1] * y[0] ) - ( x[2] * y[1] ) - ( x[0] * y[2] ) );
and the area of a planar polygon is given by
area = 0.0;
for ( i = 0; i < n - 1; i++ )
area += ( x[i] * y[i + 1] ) - ( x[i + 1] * y[i] );
area += ( x[n - 1] * y[0] ) - ( x[0] * y[n - 1] );
area /= 2.0;
If the area is a negative number, the polygon or triangle is clockwise,
if positive, it is counterclockwise. This is for the x-y plane of view.
If the polygon area is 0 then the polygon is not "viewed" from the x-y plane.
*/
poly_area_xy()
{
int c=0,d=0;
if (POLY.vertices == 3) {
area = 0.5 * ( ( (double)POLY.xyz[0][0] * (double)POLY.xyz[1][2] ) +
( (double)POLY.xyz[1][0] * (double)POLY.xyz[2][2] ) +
( (double)POLY.xyz[2][0] * (double)POLY.xyz[0][2] ) -
( (double)POLY.xyz[1][0] * (double)POLY.xyz[0][2] ) -
( (double)POLY.xyz[2][0] * (double)POLY.xyz[1][2] ) -
( (double)POLY.xyz[0][0] * (double)POLY.xyz[2][2] ) );
}
else {
area = 0.0;
for (c=0;c<POLY.vertices-1;++c)
area += ( (double)POLY.xyz[c][0] * (double)POLY.xyz[c+1][2] ) -
( (double)POLY.xyz[c+1][0] * (double)POLY.xyz[c][2] );
area += ( (double)POLY.xyz[POLY.vertices-1][0] * (double)POLY.xyz[0][2] ) -
( (double)POLY.xyz[0][0] * (double)POLY.xyz[POLY.vertices-1][2] );
area /= 2.0;
}
if (area < 0.0) { ++xycpoly; ++xypols; return(1); }
if (area > 0.0) { ++xyccpoly; ++xypols; return(1); }
if (area == 0.0) return(poly_area_yz());
return(0);
}
poly_area_yz()
{
int c=0,d=0;
if (POLY.vertices == 3) {
area = 0.5 * ( ( (double)POLY.xyz[0][1] * (double)POLY.xyz[1][2] ) +
( (double)POLY.xyz[1][1] * (double)POLY.xyz[2][2] ) +
( (double)POLY.xyz[2][1] * (double)POLY.xyz[0][2] ) -
( (double)POLY.xyz[1][1] * (double)POLY.xyz[0][2] ) -
( (double)POLY.xyz[2][1] * (double)POLY.xyz[1][2] ) -
( (double)POLY.xyz[0][1] * (double)POLY.xyz[2][2] ) );
}
else {
area = 0.0;
for (c=0;c<POLY.vertices-1;++c)
area += ( (double)POLY.xyz[c][1] * (double)POLY.xyz[c+1][2] ) -
( (double)POLY.xyz[c+1][1] * (double)POLY.xyz[c][2] );
area += ( (double)POLY.xyz[POLY.vertices-1][1] * (double)POLY.xyz[0][2] ) -
( (double)POLY.xyz[0][1] * (double)POLY.xyz[POLY.vertices-1][2] );
area /= 2.0;
}
if (area < 0.0) { ++yzcpoly; ++yzpols; return(2); }
if (area > 0.0) { ++yzccpoly; ++yzpols; return(2); }
if (area == 0.0) return(poly_area_xz());
return(0);
}
poly_area_xz()
{
int c=0,d=0;
if (POLY.vertices == 3) {
area = 0.5 * ( ( (double)POLY.xyz[0][0] * (double)POLY.xyz[1][1] ) +
( (double)POLY.xyz[1][0] * (double)POLY.xyz[2][1] ) +
( (double)POLY.xyz[2][0] * (double)POLY.xyz[0][1] ) -
( (double)POLY.xyz[1][0] * (double)POLY.xyz[0][1] ) -
( (double)POLY.xyz[2][0] * (double)POLY.xyz[1][1] ) -
( (double)POLY.xyz[0][0] * (double)POLY.xyz[2][1] ) );
}
else {
area = 0.0;
for (c=0;c<POLY.vertices-1;++c)
area += ( (double)POLY.xyz[c][0] * (double)POLY.xyz[c+1][1] ) -
( (double)POLY.xyz[c+1][0] * (double)POLY.xyz[c][1] );
area += ( (double)POLY.xyz[POLY.vertices-1][0] * (double)POLY.xyz[0][1] ) -
( (double)POLY.xyz[0][0] * (double)POLY.xyz[POLY.vertices-1][1] );
area /= 2.0;
}
if (area < 0.0) { ++xzcpoly; ++xzpols; return(3); }
if (area > 0.0) { ++xzccpoly; ++xzpols; return(3); }
if (area == 0.0) ++nonpols;
return(0);
}